package com.csust.eyediagnosis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.eyediagnosis.exception.BusinessException;
import com.csust.eyediagnosis.exception.ErrorCode;
import com.csust.eyediagnosis.exception.ThrowUtils;
import com.csust.eyediagnosis.manager.FileManager;
import com.csust.eyediagnosis.mapper.NewsMapper;
import com.csust.eyediagnosis.mapper.UserMapper;
import com.csust.eyediagnosis.model.dto.file.UploadPictureDetail;
import com.csust.eyediagnosis.model.dto.news.NewsQueryDTO;
import com.csust.eyediagnosis.model.dto.news.NewsUploadDTO;
import com.csust.eyediagnosis.model.entity.News;
import com.csust.eyediagnosis.model.entity.User;
import com.csust.eyediagnosis.model.vo.news.NewsVO;
import com.csust.eyediagnosis.service.NewsService;
import com.csust.eyediagnosis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.csust.eyediagnosis.constant.ExceptionConstant.PARAMS_IS_NULL;
import static com.csust.eyediagnosis.constant.FileConstant.PICTURE_FAIL_UPLOAD;
import static com.csust.eyediagnosis.constant.OrderConstant.ASC;

/**
* @author 钰玟
* @description 针对表【t_news(新闻)】的数据库操作Service实现
* @createDate 2025-04-23 19:14:31
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService{
    @Resource
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Resource
    private FileManager fileManager;


    @Override
    public QueryWrapper<News> getNewsQueryWrapper(NewsQueryDTO newsQueryDTO) {
        if(newsQueryDTO==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, PARAMS_IS_NULL);
        }
        //提取查询的字段
        Long newsId = newsQueryDTO.getId();
        String title = newsQueryDTO.getTitle();
        String summary = newsQueryDTO.getSummary();
        String sortField = newsQueryDTO.getSortField();
        String sortOrder = newsQueryDTO.getSortOrder();
        //构建query对象
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(newsId),"id", newsId);
        queryWrapper.like(StrUtil.isNotBlank(title),"title", title);
        queryWrapper.like(StrUtil.isNotBlank(summary),"summary", summary);
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField),sortOrder.equals(ASC),sortField);
        //返回query
        return queryWrapper;
    }

    @Override
    public List<NewsVO> getNewsVOList(List<News> newsList) {
       if (CollUtil.isEmpty(newsList)){
           return new ArrayList<>();
       }
       return newsList.stream().map(this::getNewsVO).collect(Collectors.toList());
    }

    @Override
    public NewsVO getNewsVO(News news) {
        if(news == null){
            return null;
        }

        //获取当前新闻的作者
        User user = userMapper.selectUserNameAndUserAvatarByUserId(news.getUserId());
        //设置作者名和作者头像
        String Username = user.getUserName();
        String UserAvatar = user.getUserAvatar();

        NewsVO newsVO = new NewsVO();
        BeanUtil.copyProperties(news,newsVO);

        newsVO.setCoverImage(news.getCoverImg());
        //设置作者
        newsVO.setAuthor(Username);
        newsVO.setAvatar(UserAvatar);
        return newsVO;
    }
    @Override
    public String uploadCoverImage(MultipartFile multipartFile, NewsUploadDTO newsUploadDTO, User loginUser) {
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NO_AUTH_ERROR);
        //先判断操作类型
        Long newsId = null;
        if(newsUploadDTO !=null){
            newsId = newsUploadDTO.getId();
        }
        //如果是更新图片，则应可以在数据库中查询到该图片
        if(newsId != null){
            boolean exists = this.lambdaQuery()
                    .eq(News::getId, newsId)
                    .exists();
            ThrowUtils.throwIf(!exists,ErrorCode.NOT_FOUND_ERROR, PARAMS_IS_NULL);
        }
        System.err.println(newsId);
        //上传图片
        //用户Id作为每个用户的图库前缀
        String uploadPrefix = String.format("cover/%s",loginUser.getId());
        UploadPictureDetail uploadPictureDetail = fileManager.uploadPicture(multipartFile, uploadPrefix);
        //构造要入库的图片基本信息
        News news = new News();
        news.setTitle("");
        news.setUserId(loginUser.getId());
        news.setCoverImg(uploadPictureDetail.getUrl());

        //如果pictureId 不为空 => 更新，否则是新增
        if(newsId != null){
            //如果是更新，则补充 id和编辑时间
            news.setId(newsId);
            news.setEditTime(new Date());
        }
        //插入/更新
        boolean result = this.saveOrUpdate(news);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR,PICTURE_FAIL_UPLOAD);
        return uploadPictureDetail.getUrl();
    }
}




